$(function () {
    findAll();
})

function findAll() {
    let data = {
       messageId: $("#messageId").val(),
        userName: $("#userName").val(),
        messageName: $("#messageName").val(),
        view: $("#view").val(),
        content: $("#content").val(),
        email: $("#email").val(),
        time: $("#time").val(),
        lowTime: $("#lowTime").val(),
        topTime: $("#topTime").val(),
        lowView: $("#lowView").val(),
        topView: $("#topView").val(),
    };
    console.log(data);
    $.ajax({
        url: '/back/message/findAll',
        data: data,
        type: 'post',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            setData(data.list);
        }
    })
}

function setData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += ' <div>\n' +
            '            <div>' + data[i].messageId + '</div>\n' +
            '            <div>' + replace(data[i].userName) + '</div>\n' +
            '            <div>' + replace(data[i].messageName) + '</div>\n' +
            '            <div>' + replace(data[i].view) + '</div>\n' +
            '            <div>' + replace(data[i].content) + '</div>\n' +
            '            <div>' + replace(data[i].email) + '</div>\n' +
            '            <div>' + replace(data[i].time) + '</div>\n' +
            '            <div>\n' +
            '                <div><a href="javascript:edit(' + data[i].messageId + ')"> 设置及回复</a></div>\n' +
            '                <div onclick="del(' + data[i].messageId + ')">删除</div>\n' +
            '            </div>\n' +
            '        </div>';
    }
    $(".myBody").html(html);


}

function replace(value) {
    return value == undefined ? '' : value;

}

function del(messageId) {
    $.ajax({
        url: '/back/message/del',
        data: {messageId: messageId},
        dataType: 'json',
        type: 'post',
        success: function (data) {
            console.log(data);
            if (data.result == 'success') {

                findAll();
            } else {
                alert("删除失败");
            }
        }
    })
}

function edit(id) {
    sessionStorage.setItem("messageId", id);
    localStorage.setItem("messageId", id);
    // $(".right").load("/html/user/userEdit.html");
    openWindow("留言编辑", "/html/message/messageEdit.html");
}

function openWindow(title, url) {
    $("#title").html(title);
    $("#content").load(url);
    $("#alertWindow").show();
//用户编辑信息。 分页
}

function closeWindow() {
    $("#alertWindow").hide();
}

function clearContent() {
    $("#userName").val("");
    $("#messageName").val("");
    $("#view").val("");
    $("#content").val("");
    $("#email").val("");
    $("#time").val("");
    $("#lowTime").val("");
    $("#topTime").val("");
    $("#lowView").val("");
    $("#topView").val("");
}

function Adding() {
    let da = {
        messageId: sessionStorage.getItem("messageId"),
        userName: $("#userName").val(),
        messageName: $("#messageName").val(),
        view: $("#view").val(),
        content: $("#content").val(),
        email: $("#email").val(),
        time: $("#time").val(),
        lowTime: $("#lowTime").val(),
        topTime: $("#topTime").val(),
        lowView: $("#lowView").val(),
        topView: $("#topView").val(),
    };
    // console.log(data);
    $.ajax({
        url: '/back/message/edit',//对应的是后台servlet的webServlet的注解
        data: da,//da 前端传递到后端的数据
        type: 'post',
        dataType: 'json',//
        success: function (data) {//后端响应给前台的数据

            if (data.result == 'success') {
                findAll();
                alert("添加成功");
                closeWindow();
                // $(".right").load("/html/user/userManage.html")
            } else {
                alert("添加失败");
            }
        }
    });

}
